package org.spring.springboot.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.spring.springboot.base.AppConstant;
import org.spring.springboot.pojo.RequstData;
import org.spring.springboot.pojo.ResponseData;
import org.spring.springboot.pojo.resp.Result;
import org.spring.springboot.tools.KeySecretUtil;
import org.springframework.stereotype.Service;

import com.thoughtworks.xstream.XStream;


@Service
public class HisInformationService<T> {

	   XStream xstream = AppConstant.xstreamCDATA;
	/**
	 * 根据模块名称 和 请求数据访问web服务
	 * @param modelname
	 * @param req
	 * @return
	 */
	     public Result<T> request(String modelname, RequstData req) {
			 //定义请求统一格式
	   	  xstream.alias("ROOT", RequstData.class);
			 //对象转换成XML
	   	  String data = xstream.toXML(req);
	   	  System.out.println("加密后的请求数据--------------------------------------------------");
	   	  System.out.println(data);
	   	  System.out.println("加密后的请求数据--------------------------------------------------");
			 //请求得到结果
	   	  String result = httppostrequest(AppConstant.SERVER_URL+modelname,data);
			 //结果数据返回统一格式
	   	  xstream.alias("ROOT", ResponseData.class);
	      ResponseData resultdata = (ResponseData) xstream.fromXML(result);
	      Result mResult  = new Result();
	         try {
				 //判断是否错误
	        	  if(resultdata.getSTATE() == "F") {
	        		  mResult.setState("F");
	        		  mResult.setError(resultdata.getERROR());
	        	  }else {
					  //如果正确 返回对应数据
	    			  result = KeySecretUtil.Decrypt(resultdata.getDATAPARAM(), AppConstant.keySecret);
	    			  System.out.println("得到的数据"+result);
	    			  T o = (T)xstream.fromXML(result);
	    			  mResult.setData(o);
	    			  return mResult;
	        	  }
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	         return null;
	     }
	/**
	 * http post请求web服务
	 * @param url
	 * @param data
	 * @return
	 */
	     public String httppostrequest(String url,String data) {
	    	  try {  
	              HttpPost request = new HttpPost(url);
	              List<BasicNameValuePair> params = new ArrayList();
	              params.add(new BasicNameValuePair("ReData", data));  //��ע������ĺ���������6λ��  
	              request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
	              HttpResponse httpResponse = new DefaultHttpClient().execute(request);
	              if (httpResponse.getStatusLine().getStatusCode() != 404)  
	              {  
	              	
	                      String result = EntityUtils.toString(httpResponse.getEntity());
	                      System.out.println(result);
	                      return result;
	              }  
		      } catch (Exception e) {  
		//          Log.e("eee", ""+e);  
		      e.printStackTrace();  
		      }  
	    	     return "";
		      }
}
